package com.magicodex.bookkeeping.biz.setting.event;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.view.View;
import android.widget.Toast;

import com.magicodex.bookkeeping.R;

/**
 * @author mh.z
 */
public class AboutUsButtonClickListener implements View.OnClickListener {
    private Activity activity;

    public AboutUsButtonClickListener(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onClick(View view) {
        Resources resources = activity.getResources();

        String emailLabel = resources.getString(R.string.about_us_email_label);
        String emailValue = resources.getString(R.string.about_us_email_value);
        String message = emailLabel + emailValue;

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(R.string.dialog_title_about_us);
        builder.setMessage(message);

        builder.setPositiveButton(R.string.button_copy, createPositiveButtonClickListener());
        builder.setNegativeButton(R.string.button_cancel, createNegativeButtonClickListener());

        AlertDialog dialog = builder.create();
        // 显示对话框
        dialog.show();
    }

    /**
     * 点击复制按钮的事件监听器
     *
     * @return
     */
    protected DialogInterface.OnClickListener createPositiveButtonClickListener() {
        return (dialog, which) -> {
            Resources resources = activity.getResources();
            String label = resources.getString(R.string.clip_label_email_address);
            String email = resources.getString(R.string.about_us_email_value);

            // 邮箱地址放入到剪贴板
            ClipData clipData = ClipData.newPlainText(label, email);
            ClipboardManager clipboardManager = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
            clipboardManager.setPrimaryClip(clipData);

            // 提示复制成功
            Toast toast = Toast.makeText(activity, R.string.msg_copy_success, Toast.LENGTH_SHORT);
            toast.show();
        };
    }

    /**
     * 点击取消按钮的事件监听器
     *
     * @return
     */
    protected DialogInterface.OnClickListener createNegativeButtonClickListener() {
        return (dialog, which) -> {
            //
        };
    }

}
